package me.stone.stanimeclient.binding

import android.graphics.Color
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.TextView
import androidx.databinding.BindingAdapter
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout

object BindingAdapter {

    @JvmStatic
    @BindingAdapter("swipeRefreshColors")
    fun setSwipeRefreshColors(swipeRefreshLayout: SwipeRefreshLayout, colorResIds: IntArray) {
        swipeRefreshLayout.setColorSchemeColors(*colorResIds)
    }

    @JvmStatic
    @BindingAdapter("visibleGone")
    fun showHide(view: View, show: Boolean) {
        view.visibility = if (show) View.VISIBLE else View.GONE
    }

    @JvmStatic
    @BindingAdapter("highLightKey")
    fun highLightKey(view: TextView, key: String) {
        val fullText = view.text.toString()
        val spannableString = SpannableString(fullText)
        val start = fullText.indexOf(key)
        if (start >= 0) {
            spannableString.setSpan(
                ForegroundColorSpan(Color.RED), start, start + key.length,
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE
            )
            view.text = spannableString
        }
    }

}